//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj引导页

#import "NotesGuideVC.h"
#import "TYCyclePagerView.h"
#import "MJCyclePagerViewCell.h"
@interface NotesGuideVC ()<TYCyclePagerViewDataSource,TYCyclePagerViewDelegate>
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, assign) CGFloat pageWidth;
@property (nonatomic, assign) NSInteger pageIndex;
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;
@property (weak, nonatomic) IBOutlet UIButton *leftButton;
@property (weak, nonatomic) IBOutlet UIButton *rightButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *progressBarViewW;
@property (weak, nonatomic) IBOutlet TYCyclePagerView *pagerView;

@end

@implementation NotesGuideVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self handleLastPageEvent];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.title = MJUILocalizedString(@"Operation Guide");
    [self.btnBack setImage:[UIImage imageNamed:@"icon_shanchu"] forState:0];
    
    self.imageArray = [NSMutableArray array];
    for (int i = 0; i<22; i++) {
        NSString *imageName = [NSString stringWithFormat:@"j_%d",i+1];

        if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleEN){
            imageName = [NSString stringWithFormat:@"e_%d",i+1];
        }else if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHansCN){
            imageName = [NSString stringWithFormat:@"j_%d",i+1];
        }else if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleRU){
            imageName = [NSString stringWithFormat:@"ru_%d",i+1];
        }else if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleID){
            imageName = [NSString stringWithFormat:@"id_%d",i+1];
        }else{
            imageName = [NSString stringWithFormat:@"f_%d",i+1];
        }
        [self.imageArray addObject:imageName];
    }
    self.pagerView.isInfiniteLoop = NO;
    self.pagerView.autoScrollInterval = 4;
    self.pagerView.dataSource = self;
    self.pagerView.delegate = self;
    
    // registerClass or registerNib
    UINib *nib = [UINib nibWithNibName:@"MJCyclePagerViewCell" bundle:nil];
    [self.pagerView registerNib:nib forCellWithReuseIdentifier:@"MJCyclePagerViewCell"];
    
    self.pageWidth = (SCREEN_WIDTH - 50)/self.imageArray.count;
    self.progressBarViewW.constant = self.pageWidth;
    
    //
    [self.pauseButton setImage:MJUIImageMake(@"icon_zanting") forState:0];
    [self.pauseButton setImage:MJUIImageMake(@"icon_bofang_g") forState:UIControlStateSelected];
    
    [self.leftButton setImage:MJUIImageMake(@"icon_zuojiantou") forState:0];
    [self.leftButton setImage:MJUIImageMake(@"icon_zuojiantou2") forState:UIControlStateSelected];
    
    [self.rightButton setImage:MJUIImageMake(@"icon_youjiantou2") forState:0];
    [self.rightButton setImage:MJUIImageMake(@"icon_youjiantou") forState:UIControlStateSelected];
    MJWeakSelf
   
    [self.leftButton mj_addAction:^(UIButton *button) {
        if(!weakSelf.leftButton.isSelected){
            [weakSelf.pagerView scrollToItemAtIndex:weakSelf.pageIndex - 1 animate:true];
        }
    }];
    
    [self.rightButton mj_addAction:^(UIButton *button) {
        if(!weakSelf.rightButton.isSelected){
            [weakSelf.pagerView scrollToItemAtIndex:weakSelf.pageIndex + 1 animate:true];
        }else{
            //到底了
//            [weakSelf handleLastPageEvent];
        }
    }];

}

#pragma mark - 方法
- (IBAction)handlePauseButtonClick:(UIButton *)sender {
    [sender setSelected:!sender.isSelected];
    if(sender.isSelected){
        self.pagerView.timer.fireDate = NSDate.distantFuture;
    }else{
        self.pagerView.timer.fireDate = [NSDate new];
    }
}

- (void)handleLastPageEvent {
    MJWeakSelf
    MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
    configure.confirmButtonColor = UIColor.mj_appColor;
    configure.confirmTitle = MJUILocalizedString(@"Exit");
    configure.cancelTitle = MJUILocalizedString(@"Review");
    configure.messageAlignmentCenter = true;
    configure.disableDismissWindow = true;
    configure.isBottom = true;
    
    NSString *messageString = MJUILocalizedString(@"You have reached the last page. Do you want to exit?");
    [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Exit Playback") message:messageString configure:configure confirmBlock:^{
        
        [weakSelf mj_popViewController];

    } cancelBlock:^{
        [weakSelf handlePauseButtonClick:weakSelf.pauseButton];

    }];
}

#pragma mark - 代理
#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.imageArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {

    MJCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"MJCyclePagerViewCell" forIndex:index];
    NSString *imageName = self.imageArray[index];
    if(index == 5 || index == 8 || index == 13 || index == 18 || index == 21){
        cell.iconImageView.image = [UIImage mj_imageWithGIFNamed:imageName];
    }else{
        cell.iconImageView.image = [UIImage mj_imageWithBundleNamed:imageName ofType:@"jpg"];
    }
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = pageView.bounds.size;
    layout.layoutType = TYCyclePagerTransformLayoutNormal;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    //[_pageControl setCurrentPage:newIndex animate:YES];
    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
    self.pageIndex = toIndex;
    self.progressBarViewW.constant = self.pageWidth*(toIndex + 1);

    if(toIndex == 0){
        self.pagerView.autoScrollInterval = 4;
        [self.leftButton setSelected:true];
    }else{
        [self.leftButton setSelected:false];
        self.pagerView.autoScrollInterval = 2.5;
    }
    
    if(toIndex == self.imageArray.count - 1){
        [self.rightButton setSelected:true];
        
        self.pagerView.timer.fireDate = NSDate.distantFuture;
        [self.pauseButton setSelected:true];

//        [self handlePauseButtonClick:self.pauseButton];
//        [self handleLastPageEvent];
        
        MJWeakSelf
        //最后一个弹出弹窗
        MJUIAfter_GCD(3, ^{
            NSLog(@"pageIndex=======================%ld",weakSelf.pageIndex);
            if(weakSelf){
                NSLog(@"pageIndex=====111111==================%ld",weakSelf.pageIndex);
                if(weakSelf.pageIndex == weakSelf.imageArray.count - 1){
                    [weakSelf handleLastPageEvent];
                }
            }
        });
    }else{
        [self.rightButton setSelected:false];
    }
    
    //暂停
    if(self.pauseButton.isSelected){
        self.pagerView.timer.fireDate = NSDate.distantFuture;
    }
}

@end
